﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DevExpress.XtraGrid.Views.Base;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace System
{
    public static class GridExt
    {
        public static DataTable GetTable(this BaseView view)
        {
            DataTable dt = null;
            if (view != null)
            {
                if (view.DataSource is DataTable)
                    return view.DataSource as DataTable;
                if (view.DataSource is DataView)
                    return (view.DataSource as DataView).Table;
            }
            return dt;
        }
        public static DataTable GetSourceTableForView(this DevExpress.XtraGrid.Views.Base.BaseView view)
        {
            if (view.GridControl != null)
            {
                DataSet ds = null;
                string name = view.GridControl.DataMember;

                if (view.GridControl.DataSource is DataSet)
                {
                    ds = view.GridControl.DataSource as DataSet;
                }
                else
                {
                    if (view.GridControl.DataSource is DataTable)
                        return view.GridControl.DataSource as DataTable;
                    else if (view.GridControl.DataSource is BindingSource)
                    {
                        BindingSource s = view.GridControl.DataSource as BindingSource;
                        if (s.DataSource is DataTable)
                            return s.DataSource as DataTable;
                        else
                            if (s.DataSource is DataSet)
                            {
                                ds = s.DataSource as DataSet;
                                if (s.DataMember.IsNotNull())
                                    name = s.DataMember;
                            }
                    }
                }
                if (ds != null)
                {
                    if (name.IsNotNull() && ds.Tables.Contains(name) &&
                        (view == view.GridControl.MainView || view.SourceView == view.GridControl.MainView))
                    {
                        return ds.Tables[name];
                    }
                    else
                    {
                        GridLevelNode node = view.GridControl.LevelTree.Find(view);
                        if (node != null && node.RelationName.IsNotNull() && ds.Relations.Contains(node.RelationName))
                        {
                            return ds.Relations[node.RelationName].ChildTable;
                        }
                    }
                }
            }
            return null;
        }
        public static DataRow[] GetSelectadDataRows(this GridView view)
        {
            int[] idx = view.GetSelectedRows();
            if (idx == null || idx.Length == 0)
            {
                idx = new int[1];
                idx[0] = view.FocusedRowHandle;
            }
            List<DataRow> list = new List<DataRow>();
            foreach (int i in idx)
            {
                DataRow r = view.GetDataRow(i);
                if (r != null)
                    list.Add(r);
            }
            return list.ToArray();
        }

        public static void ExpandRow(this DevExpress.XtraGrid.Views.Grid.GridView view, DataRow row, bool expand = true)
        {
            try
            {
                int i = view.GetRowHandle(row.Table.Rows.IndexOf(row));
                view.SetMasterRowExpanded(i, expand);
            }
            catch { }

        }
    }
}
